import {
	isArray,
	isObject,
	isTrue,
	isFunction,
	generateUUID,
	accAdd
} from '@/utils/util.js'
import {
	cloneDeep
} from 'lodash'

// 处理原始set
function handleSet(set) {
	let pointIdMap = {}
	if (isObject(set.to)) {
		const keys = Object.keys(set.to)
		console.log(keys)
		if (isArray(keys) && keys.length > 0) {
			for (let key in keys) {
				let key_arr = key.split('_')
				if (set.to[key] > 0) {
					pointIdMap[key_arr[1]] = true
				}
			}
		}
	}
	console.log('pointIdMap', pointIdMap)
	let one = {}
	let floor = {}
	const pointIdKeys = Object.keys(pointIdMap)
	if (isArray(pointIdKeys) && pointIdKeys.length > 0) {
		if (isObject(set.one_limit)) {
			const keys = Object.keys(set.one_limit)
			if (isArray(keys) && keys.length > 0) {
				for (let pointIdKey in pointIdKeys) {
					one[pointIdKey] = set.one_limit[pointIdKey] || false
				}
			}
		}
		if (isObject(set.floor_limit)) {
			const keys = Object.keys(set.floor_limit)
			if (isArray(keys) && keys.length > 0) {
				for (let pointIdKey in pointIdKeys) {
					floor[pointIdKey] = set.floor_limit[pointIdKey] || 1
				}
			}
		}
	}
	set.one = cloneDeep(one)
	set.floor = cloneDeep(floor)
	return set
}

// 检查限制是否冲突
function checkLimitIsNotClash(one, two) {
	let flag = true
	if (isObject(one.one) && isObject(one.floor) && isObject(two.one) && isObject(two.floor)) {
		const oneKeys1 = Object.keys(one.one)
		if (isArray(oneKeys1) && oneKeys1.length > 0) {
			for (let oneKey in oneKeys1) {
				if (two.one[oneKey] != undefined && one.one[oneKey] != two.one[oneKey]) flag = false
			}
		}
		const oneKeys2 = Object.keys(two.one)
		if (isArray(oneKeys2) && oneKeys2.length > 0) {
			for (let oneKey in oneKeys2) {
				if (one.one[oneKey] != undefined && one.one[oneKey] != two.one[oneKey]) flag = false
			}
		}
		const floorKeys1 = Object.keys(one.floor)
		if (isArray(floorKeys1) && floorKeys1.length > 0) {
			for (let floorKey in floorKeys1) {
				if (two.floor[floorKey] != undefined && one.floor[floorKey] != two.floor[floorKey]) flag = false
			}
		}
		const floorKeys2 = Object.keys(two.floor)
		if (isArray(floorKeys2) && floorKeys2.length > 0) {
			for (let floorKey in floorKeys2) {
				if (one.floor[floorKey] != undefined && one.floor[floorKey] != two.floor[floorKey]) flag = false
			}
		}
	}
	return flag
}

function mergeSet(one, two) {
	return one
}

export function mergeSetttings(settings, curSet) {
	if (isArray(settings) && settings.length > 0) {
		let cur = handleSet(curSet)
		for(let index=0; index < settings.length; index++){
			let setting = settings[index]
			let set = handleSet(setting)
			// 存在可合并项：时间段相同，且两个限制不冲突
			if (isArray(set.date_range) && set.date_range.length == 2 &&
				isArray(cur.date_range) && cur.date_range.length == 2) {
				let set_date_ranges = cloneDeep(set.date_range)
				let cur_date_ranges = cloneDeep(cur.date_range)
				if(set_date_ranges[0] == cur_date_ranges[0] && set_date_ranges[1] == cur_date_ranges[1] && checkLimitIsNotClash(set, cur)){
					console.log('set',set)
					console.log('cur',cur)
					let merge_set = mergeSet(set, cur)
					settings.splice(index, 1, merge_set)
					break;
				}
			}
		}
	}
}
